

package controller;

import java.util.List;

import model.User;
import Repository.UserRepository;



public class LoginController 
{
	
	private String userName;
	private String password;
	private User userCredential;
	
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	
	// This funciton authenticates a user
	
	public boolean authenticate(){
		userCredential = UserRepository.getUser(userName);
		
		System.out.println("Authenticating user..."  );
		
		if(userCredential!=null){
			if(userCredential.getUserName().equals(userName) && 
					userCredential.getPassword().equals(password))
			{
				System.out.println("Welcome "+ userName.toUpperCase() + "("+userCredential.getRole()+")");
				return true;
			}
			
		}
		return false;
		
	}
	
	
	public List<String> getUserActions(){
		return userCredential.getUserActions();
		
	}
	
	public User getUser(){
		return userCredential;
	}
	
	public boolean isAuthorized(String action){
		List userActions = userCredential.getUserActions();
		
		for(Object obj : userActions){
			String strAction = (String)obj;
			if(action.toLowerCase().contains(strAction.toLowerCase()))
			return true;
		}
				
		return false;
		
	}
	

}
